﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace T11Func
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("求两数之和：{0}",MyMath.Sum (1,2));

            Console.WriteLine("求多个数之和{0}",MyMath.Sum(1,2,3,4,5,-2,-1,6,7,8,9,10));

            Console.WriteLine("求多个数之和{0}",MyMath .Sum(1.0,2.0));

            int[] number = { 1, 2, 3, 4, 5,-1,100,6, 7, 8, 9, 10 };
            int? maxIndex = null;//ref必须有初始值
            int? max = MyMath.Max(number, ref maxIndex);
            Console.WriteLine("最大值为：{0},索引为：{1}",max,maxIndex);
            int? max2 = MyMath. Max(new int[0], ref maxIndex );
            Console.WriteLine("最大值为：{0},索引为：{1}", max2, maxIndex);

            int? minIndex;//out可以没有初始值
            int? min = MyMath.Min(number, out minIndex);
            Console.WriteLine("最小值为：{0}，索引为：{1}",min,minIndex );
            int? min2 = MyMath.Min(new int[0], out minIndex);
            Console.WriteLine("最小值为：{0}，索引为：{1}", min2, minIndex);

            Console.ReadKey();
        }

        /// <summary>
        /// 自定义数学函数库
        /// </summary>
       
        

        class MyMath
        {
            /// <summary>
            /// 求两个数的和
            /// </summary>
            /// <param name="a">操作数一</param>
            /// <param name="b">操作数二</param>
            /// <returns>两个数之和</returns>
            public static int Sum(int a,int b)
            {
                //int result=0;
                //result = a + b;
                //return result;

                return a + b;


            }

            /// <summary>
            /// 求多个数和
            /// </summary>
            /// <param name="values">操作数集合（params 可变长参数列表0或n个）</param>
            /// <returns>多个数之和(可空值)</returns>
            public static int? Sum(params int[] values)
            {
                int? result = null;

                if (values.Length > 0)
                {
                    result = 0;

                    //局部变量可使用var定义变量类型推断
                    foreach (var value in values)
                    {
                        result += value;


                    }

                }
                return result;

            }

            /// <summary>
            /// 求多个数和
            /// 求 更多不同类型的数据之和 --- 泛型
            /// </summary>
            /// <param name="values">操作数集合（params 可变长参数列表0或n个）</param>
            /// <returns>多个数之和(可空值)</returns>
            public static double? Sum(params double[] values)
            {
                double? result = null;

                if (values.Length > 0)
                {
                    result = 0;

                    //局部变量可使用var定义变量类型推断
                    foreach (var value in values)
                    {
                        result += value;


                    }

                }
                return result;

            }
            /// <summary>
            /// 求一组数得最大值
            /// </summary>
            /// <param name="values">数据集合</param>
            /// <param name="maxIndex">最大值的索引</param>
            /// <returns>最大值</returns>
            public static int? Max(int[] values,ref int?maxIndex)
            {
                if (values.Length <= 0)
                {
                    maxIndex = null;
                    return null;
                }
                else
                {
                    var max = values[0];
                    maxIndex = 0;
                    for (int i = 0; i < values .Length; i++)
                    {
                        if (values[i]>max)
                        {
                            max = values[i];
                            maxIndex = i;


                        }

                    }
                    return max;
                }
            }


            /// <summary>
            /// 求一组数的最小值
            /// </summary>
            /// <param name="values">数据集合</param>
            /// <param name="minIndex">最小值索引</param>
            /// <returns>最小值</returns>
            public static int? Min(int[] values, out int? minIndex)
            {
                if (values.Length  <= 0)
                {
                    minIndex = null;
                    return null;

                }
                else
                {
                    var min=values[0];

                    minIndex = 0;

                    for (int i = 0; i < values.Length; i++)
                    {
                        if (values[i] <=min)
                        {
                            min = values[i];
                            minIndex = i;
                        }
                    }

                    return min;
                }
            }
        }
    }
}
